﻿/*----------------------------------------------------------------
// 公司名称：公司名称
//
// 产品名称：Gaf
//
// 创 建 人：netcasewqs
// 创建日期：2022/3/4
// 邮    箱：netcasewqs@qq.com
//
// Copyright (C) 2022 Gaf，保留所有权利。
//
//----------------------------------------------------------------*/

namespace Gaf.Impl;

sealed class LogListener : IDisposable
{
    private GafImpl _gaf;
    public LogListener(GafImpl gaf)
    {
        _gaf = gaf;
        _gaf.AddinStateChanged += OnAddinStateChanged;
        _gaf.FramewormStateChanged += OnFramewormStateChanged;
        _gaf.FrameworkErrorOccurred += OnFrameworkErrorOccurred;
    }

    private void OnFrameworkErrorOccurred(IGaf e, Exception ex)
    {
        Log.Error(ex.Message, ex);
    }

    private void OnFramewormStateChanged(IGaf e)
    {
        Log.Info($"Gaf framework state Changed:{e.State}");
    }

    private void OnAddinStateChanged(IAddin e)
    {
        Log.Info($"Addin[{e.Id}-'{e.Name}'] state changed:{e.State}");
    }

    public void Dispose()
    {
        _gaf.AddinStateChanged -= OnAddinStateChanged;
        _gaf.FramewormStateChanged -= OnFramewormStateChanged;
        _gaf.FrameworkErrorOccurred -= OnFrameworkErrorOccurred;
    }
}